import numpy as np
import matplotlib.pyplot as plt

# 设置随机种子以确保结果可复现
np.random.seed(42)

# 模拟数据集
n_samples, n_features = 50, 10
X = np.random.randn(n_samples, n_features)

true_weights = np.array([1, 0.5, 0] + [0] * (n_features - 3))  # 只有前三个特征是重要的
y = np.dot(X, true_weights) + np.random.randn(n_samples) * 0.5  # 添加一些噪声
print(y)
# 初始化参数
weights = np.random.randn(n_features)
learning_rate = 0.01
lambda_l1 = 0.1  # L1 正则化强度
iterations = 1000

# 存储每次迭代后的权重值用于可视化
weight_history = []

for i in range(iterations):
    y_pred = np.dot(X, weights)

    # 计算梯度（不包含正则化项）
    gradient = 2 * np.dot(X.T, ( y_pred - y )) / n_samples

    # 添加 L1 正则化梯度
    l1_gradient = lambda_l1 * weights

    # 更新权重
    weights -= learning_rate * (gradient + l1_gradient)

    # 记录当前权重
    weight_history.append(weights.copy())

# 将历史权重转换为数组以便于绘图
weight_history = np.array(weight_history)

# 绘制每个权重随迭代次数的变化
plt.figure(figsize=(14, 7))
for i in range(n_features):
    plt.plot(weight_history[:, i], label=f'Weight {i + 1}')
plt.axhline(y=0, color='black', linestyle='--')
plt.xlabel('Iteration')
plt.ylabel('Weight Value')
plt.title('Weight Evolution with L1 Regularization')
plt.legend()
plt.show()

print("最终的权重：", weights)